/**
 * @projectName study_java
 * @package Memento
 * @className Memento.UserRole
 */
package Memento;

import lombok.Data;

/**
 * UserRole
 * @description 用户角色类
 * @author answer_wx
 * @date 2022/6/29 18:10
 * @version 1.0
 */
@Data
public class UserRole {
    /**
     * 角色属性
     */
    private int healthValue;
    private int magicValue;
    private int defenceValue;
    private int attackValue;
    /**
     * 角色状态
     */
    private boolean state = false;

    /**
     * 初始化
     */
    public void initRole() {
        this.healthValue = 10000;
        this.magicValue = 3000;
        this.defenceValue = 100;
        this.attackValue = 200;
    }

    /**
     * 战斗
     */
    public void fight() {
        this.setHealthValue(getHealthValue() - (int) (Math.random() * 100));
        this.setMagicValue(getMagicValue() - (int) (Math.random() * 30));
        this.setDefenceValue(getDefenceValue() - (int) (Math.random() * 5));
        this.setAttackValue(getAttackValue() - (int) (Math.random() * 5));
    }

    /**
     * 备份
     * @return
     */
    public UserRoleMemento createMemento() {
        if (!this.state) {
            this.setState(true);
            return new UserRoleMemento(getHealthValue(), getMagicValue());
        } else {
            System.out.println("用户角色无法备份");
        }
        return null;
    }

    /**
     * 恢复
     */
    public void recovery(UserRoleMemento memento) {
        if (this.state) {
            this.healthValue = memento.getHealthValue();
            this.magicValue = memento.getMagicValue();
            this.setState(false);
        } else {
            System.out.println("用户角色无法恢复");
        }
    }

    @Override
    public String toString() {
        return "UserRole{" +
                "healthValue=" + healthValue +
                ", magicValue=" + magicValue +
                ", defenceValue=" + defenceValue +
                ", attackValue=" + attackValue +
                ", state=" + state +
                '}';
    }
}